<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/11/16
 * Time: 15:43
 *
 * 登录功能的控制器
 *
 *
 */

namespace app\index\controller;



use think\Controller;

class User extends Common
{




    /*
     * 获得登录的信息
     * @param string token
     *
     * */
    public function getUser(){
        $token = input('token');
        $usermsg = cache($token);
        if (!empty($usermsg)){
            $openid = $usermsg['openid'];
            $con['openid']=trim($openid);
            $user = new  \app\index\model\User();
            $u = $user->where($con)->find();
            if (empty($u)){
                rjson(110,'获取用户失败');
            }else{
                rjson(100,'登录用户信息',$u);
            }
        }else{
            rjson(110,'获取用户失败');
        }

    }

    /*
     * [用户资料修改]
     *  @param string uid
     *  @param string username
     *  @param string age
     *  @param string height
     *  @param string weight
     *  @param string sex
     * */
    public function modify(){

        if (request()->isPost()){
            $send = input('post.','','htmlspecialchars,strip_tags,trim');
            if (empty($send['uid'])){
                rjson(110,'uid不可以为空');
            }
            $data = array();
            $uservalidate = new \app\index\validate\User();
            if($uservalidate->scene('modify')->check($send)){
                if (!empty($send['username'])){
                    $data['username'] = $send['username'];
                }
                if (!empty($send['age'])){
                    $data['age'] = $send['age'];
                }
                if (!empty($send['height'])){
                    $data['height'] = $send['height'];
                }
                if (!empty($send['weight'])){
                    $data['weight'] = $send['weight'];
                }
                if (!empty($send['sex'])){
                    $data['sex'] = $send['sex'];
                } 
                if (!empty($send['phone'])){
                    $data['phone'] = $send['phone'];
                }
                $usermodel = new \app\index\model\User();
                $res = $usermodel->isUpdate(true)->save($data,[
                    'uid'=>$send['uid']
                ]);
                if ($res != false ){
                    rjson(100,'修改成功',$send);
                }else{
                    rjson(110,'修改失败');
                }



            }else{
                rjson(110,$uservalidate->getError());
            }







        }else{
            rjson(110,'必须post请求');
        }

    }





}